package PlayerStates;

import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import Cards.AuctionCard;
import Cards.Card;
import Cards.DealCard;
import Cards.MailCardBill;
import GameFiles.Days;
import GameFiles.GameManager;
import GameFiles.StateBase;
import Gui.GameFrame;
import Gui.GamePanel;
import Main.Defines;
import Gui.Dialogs.AuctionDialog;
import Gui.Dialogs.DealCardDialog;
import Gui.Dialogs.EventDayDialog;
import Gui.Dialogs.LotteryDayDialog;
import Gui.Dialogs.MailCardBillDialog;
import Gui.Dialogs.PayDayDialog;
import Gui.Dialogs.SellDealCardDialog;

public class HandleDay extends StateBase {

	private static HandleDay ref;

	private HandleDay() {
	}

	public static HandleDay getInstance() {
		if (ref == null) {
			ref = new HandleDay();
		}
		return ref;
	}

	@Override
	public boolean Draw(GameFrame gf) {
		GamePanel.getInstance().update();
		GameFrame.getInstance().add(GamePanel.getInstance());
		return false;
	}

	@Override
	public boolean Enter() {
		GameFrame.getInstance().update();
		GameFrame.getInstance().add(GamePanel.getInstance());
		// Look at Days class for enum listing
		// Change this switch to target specific days no matter what the player
		// is on (to debug)
		int day = Defines.GM.getDayList().elementAt(Defines.CP().getDay())
				.getDayType();
		Card DrawnCard = null;
		System.out.printf("%s\n", Defines.GM.getDayList().elementAt(
				Defines.CP().getDay()).getDayName());

		ArrayList<Integer> daysToCheck = new ArrayList<Integer>();
		daysToCheck.add(Days.BUYERDAY);
		daysToCheck.add(Days.DEALDAY);
		Random rand = new Random();
		// day = daysToCheck.get(rand.nextInt(daysToCheck.size()));
		DrawnCard = GameManager.getInstance().getDealCards().get(rand.nextInt(Defines.GM.getDealCards().size()));
		switch (/*Days.DEALDAY*/ day ) {
			case Days.STARTDAY:
				break;
			case Days.MAILDAY: {

				// Get ready to draw mail cards
				// We'll use a Vector to hold the bills, and an int to hold
				// buyers.
				Vector<Card> BillPile = new Vector<Card>();
				Days today = Defines.GM.getDayList().elementAt(
						Defines.CP().getDay());
				int numBuyers = 0;

				// Draw a number of cards equal to the mail day's value
				for (int i = 0; i < today.getValue(); i++) {

					// Draw the card
					DrawnCard = Defines.GM.getMailCards().elementAt(
							Defines.GM.rand.nextInt(Defines.GM.getMailCards()
									.size()));

					// Switch on card type
					switch (DrawnCard.getM_cardType()) {
						case Card.MailCardBill:
							// Push the bill onto the HandleDay.Enter bill pile.
							BillPile.add(new MailCardBill(DrawnCard.getName(),
									DrawnCard.getValue(),
									((MailCardBill) DrawnCard).getCost()));
							// and onto the player's bill pile.
							Defines.CP().addMailCard((MailCardBill) DrawnCard);
							break;
						case Card.MailCardBuyer:
							// Increment number of buyers
							numBuyers++;

							break;
						default:
							break;
					}
				}

				// Now process the bill pile and the buyers
				if (BillPile.size() > 0) {
					@SuppressWarnings("unused")
					MailCardBillDialog showDialog = new MailCardBillDialog(
							GameFrame.getInstance(), /* (MailCardBill) */
							BillPile/* .get(0) */);
					// Defines.CP().addMailCard((MailCardBill) BillPile.get(0));
					// Defines.EVM.queueEvent("EVENT_FINAL_LOAD");
				}
				if (numBuyers > 0) {
					@SuppressWarnings("unused")
					SellDealCardDialog sdcd = new SellDealCardDialog(numBuyers);
				}
				break;

			}
			case Days.EVENTDAY:
				// Draw an event card
				// Switch on event to call appropriate event dialog
				// Currently, only you pay or you receive events
				// for other events, see
				// http://www.centralconnector.com/GAMES/payday.html
				// 
				// Event Card Types:
				// take money from player
				// take money from player + 1 other player
				// give money to bonus pile
				// you + 1 player give money to bonus pile
				// you get money from bank
				// you +1 other player get money from bank
				//
				DrawnCard = Defines.GM.getEventCards().elementAt(
						Defines.GM.rand.nextInt(Defines.GM.getEventCards()
								.size()));

				switch (DrawnCard.getM_cardType()) {
					case Card.EventCardGain:
					case Card.EventCardLoss:
						// TODO Call the Eventcard dialog
						@SuppressWarnings("unused")
						EventDayDialog edd = new EventDayDialog(DrawnCard);
						break;
				}
				break;

			case Days.DEALDAY: {
				// Look how DrawnCard is typecast as a deal card and passed to
				// DealCardDialog
				// DealCardDialog now has that card and can display its
				// information
//				DrawnCard = Defines.GM.getDealCards().elementAt(Defines.GM.rand.nextInt(Defines.GM.getDealCards().size()));
//				if (DrawnCard.getM_cardType() == Card.DealCardAuction) {
//					AuctionDialog showDialog = new AuctionDialog((AuctionCard)DrawnCard);
//				}
//				else if (DrawnCard.getM_cardType() == Card.DealCard) {
//					@SuppressWarnings("unused")
//					DealCardDialog showDialog = new DealCardDialog(
//							(DealCard) DrawnCard);
//				}
				AuctionDialog showDialog = new AuctionDialog((DealCard)DrawnCard);
				break;
			}
			

			case Days.LOTTERYDAY:
				@SuppressWarnings("unused")
				LotteryDayDialog showDialog = new LotteryDayDialog();
				break;

			case Days.BUYERDAY:
				Days today = Defines.GM.getDayList().elementAt(
						Defines.CP().getDay());
				@SuppressWarnings("unused")
				SellDealCardDialog sdcd = new SellDealCardDialog(today
						.getValue());
				break;
			case Days.BIRTHDAY:
				break;
			case Days.YARDSALEDAY:
				break;
			case Days.PAYDAY:
				@SuppressWarnings("unused")
				PayDayDialog showDialog1 = new PayDayDialog();
				break;
			case Days.NUM_DAYTYPES:
			default:
				System.out.println("Not a Day ");
				Defines.EVM.queueEvent("EVENT_INITIAL_LOAD");
				return false;
				// break;
		}
		Defines.EVM.queueEvent("EVENT_FINAL_LOAD");
		return false;
	}

	@Override
	public boolean Execute() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean Exit() {
		// TODO Auto-generated method stub
		return false;
	}

}
